package com.googlecode.chigs.web.doku.version;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.chigs.domain.Product;
import com.googlecode.chigs.domain.Variant;
import com.googlecode.chigs.domain.Version;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigs.repository.GenericDomainService;
import com.googlecode.chigs.web.common.IconFactory;
import com.googlecode.chigs.web.common.forms.ShinyForm;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
@AuthorizeInstantiation("ROLE_USER")
public abstract class EditOrCreateVersionWindowContent extends Panel {
	@SpringBean
	private ChigsFacade chigs;

	@SpringBean
	private GenericDomainService genericDomainService;
	
	private final Product product;


	public EditOrCreateVersionWindowContent(String id, IModel<Version> model, Product product) {
		super(id);
		this.product = product;

        add(new EditOrCreateVersionForm("form", new CompoundPropertyModel<Version>(model.getObject())));
	}

	protected abstract  void afterSubmit(AjaxRequestTarget target, IModel<Version> model);

	protected abstract  void afterCanel(AjaxRequestTarget target);
	

	class EditOrCreateVersionForm extends ShinyForm<Version> {
		public EditOrCreateVersionForm(String id, IModel<Version> model) {
			super(id, new CompoundPropertyModel<Version>(model.getObject()));
			StringResourceModel lMajor = new StringResourceModel("major.label", EditOrCreateVersionForm.this, null);
			add(new Label("major.label", lMajor));
			add(new TextField<Integer>("major")
					.setRequired(true)
					.setLabel(lMajor));

			StringResourceModel lMinor = new StringResourceModel("minor.label", EditOrCreateVersionForm.this, null);
			add(new Label("minor.label", lMinor));
			add(new TextField<Integer>("minor")
					.setRequired(true)
					.setLabel(lMinor));
			
			List<Variant> products = genericDomainService.loadAll(Variant.class);
			DropDownChoice<Variant> dd = new DropDownChoice<Variant>("variant", new PropertyModel<Variant>(getModelObject(), "variant"), products, new ChoiceRenderer<Variant>("name", "id"));
			dd.setNullValid(true);
			add(dd);
			AjaxSubmitLink submitLink = new AjaxSubmitLink("submitButton") {
				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					try {
						Version version = EditOrCreateVersionForm.this.getModelObject();
						version.setProduct(product);
						chigs.saveVersion(version);						
						getSession().getFeedbackMessages().info(EditOrCreateVersionForm.this, new StringResourceModel("version.save.success", EditOrCreateVersionWindowContent.this, null).getString());
						EditOrCreateVersionWindowContent.this.afterSubmit(target, EditOrCreateVersionForm.this.getModel());
					} catch (Exception e) {
						getSession().getFeedbackMessages().error(EditOrCreateVersionForm.this, e.getMessage());
						EditOrCreateVersionWindowContent.this.afterSubmit(target, EditOrCreateVersionForm.this.getModel());
					}
				}
			};
			submitLink.add(new Image("icon", IconFactory.createOkIconReference()));
			add(submitLink);
			
			AjaxLink<Void> cancelLink = new AjaxLink<Void>("cancelButton") {
				@Override
				public void onClick(AjaxRequestTarget target) {
					EditOrCreateVersionWindowContent.this.afterCanel(target);
				}
			};
			cancelLink.add(new Image("icon", IconFactory.createCancelIconReference()));
			add(cancelLink);
			
		}
	}

}

